package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.*;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.consts.FormConstants;
import com.ccp.dev.form.model.DataTemplate;
import com.ccp.dev.form.model.FormDef;
import com.ccp.dev.form.model.FormField;
import com.ccp.dev.form.model.FormTable;
import com.ccp.dev.form.service.*;
import com.ccp.dev.form.util.CommonVar;
import com.ccp.dev.form.util.FormUtil;
import com.ccp.dev.form.util.PlatformType;
import com.ccp.dev.form.xml.constant.XmlConstant;
import com.ccp.dev.system.model.GlobalType;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.GlobalTypeService;
import com.ccp.dev.workflow.model.BpmNodeSet;
import com.ccp.dev.workflow.service.BpmNodeSetService;
import net.sf.json.JSONObject;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义表单控制器类
 *
 * @author zhaowj
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/formDef")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class FormDefController extends SysBaseController {

    @Resource
    private FormDefService formDefService;

    @Resource
    private GlobalTypeService globalTypeService;

    @Resource
    private FormTableService formTableService;

    @Resource
    private FormFieldService formFieldService;

    @Resource
    private FormRightsService formRightsService;

    @Resource
    private DataTemplateService dataTemplateService;

    @Resource
    private BpmNodeSetService bpmNodeSetService;

    /**
     * 查询条件跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/queryField")
    @Action(description = "业务数据模板中查询条件页面跳转", exectype = "页面跳转日志")
    public String queryFieldView(HttpServletRequest request, ModelMap modelMap) {
        String tableId = RequestUtil.getString(request, "tableId");
        modelMap.put("tableId", tableId);
        return getAutoView();
    }

    /**
     * 排序字段跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/sortField")
    @Action(description = "业务数据模板中排序字段页面跳转", exectype = "页面跳转日志")
    public String sortFieldView(HttpServletRequest request, ModelMap modelMap) {
        String tableId = RequestUtil.getString(request, "tableId");
        modelMap.put("tableId", tableId);
        return getAutoView();
    }

    /**
     * 子表数据授权页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/subRightsDialog")
    @Action(description = "子表数据授权页面跳转", exectype = "页面跳转日志")
    public String viewSubRightsDialog(HttpServletRequest request, Model model) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String formKey = RequestUtil.getString(request, "formKey");
        String tableId = RequestUtil.getString(request, "tableId");

        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId", "");

        model.addAttribute("procDefId", procDefId);
        model.addAttribute("nodeId", nodeId);
        model.addAttribute("formKey", formKey);
        model.addAttribute("tableId", tableId);
        model.addAttribute("parentProcDefId", parentProcDefId);
        //操作日志功能完善
        return getAutoView();
    }

    /**
     * 数据列表页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/list")
    @Action(description = "自定义表单列表页面跳转", exectype = "页面跳转日志")
    public String formDefViewList() {
        return getAutoView();
    }

    /**
     * 跳转 导入页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/import")
    @Action(description = "自定义表单导入页面跳转", exectype = "页面跳转日志")
    public String importView() {
        return getAutoView();
    }

    /**
     * 跳转 过滤窗口页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/filterScript")
    @Action(description = "业务数据模板中过滤条件脚本页面跳转", exectype = "页面跳转日志")
    public String filterScript(HttpServletRequest request, ModelMap modelMap) {
        String tableId = RequestUtil.getString(request, "tableId");
        Integer add = RequestUtil.getInt(request, "add");
        String id = RequestUtil.getString(request, "id");
        modelMap.put("tableId", tableId);
        modelMap.put("add", add);
        modelMap.put("id", id);
        return getAutoView();
    }

    /**
     * 跳转 业务数据模板 编辑模板页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/editingTemplate")
    @Action(description = "业务数据模板中编辑模板页面跳转", exectype = "页面跳转日志")
    public String editingTemplate(HttpServletRequest request, ModelMap modelMap) {
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 跳转 打印模板页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/printTemplate")
    @Action(description = "自定义表单打印模板页面跳转", exectype = "页面跳转日志")
    public String printTemplate(HttpServletRequest request, ModelMap modelMap) {
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 跳转 表单选择页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formSelector")
    @Action(description = "表单选择页面", exectype = "页面跳转日志")
    public String viewFormSelector(Model model, HttpServletRequest request) {
        String formType = RequestUtil.getString(request, "formType");
        model.addAttribute("formType", formType);
        return getAutoView();
    }

    /**
     * 跳转字段对话框页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/fieldDialog")
    @Action(description = "字段对话框页面", exectype = "页面跳转日志")
    public String viewFieldDialog(Model model, HttpServletRequest request) {
        String formKey = RequestUtil.getString(request, "formKey");
        model.addAttribute("formKey", formKey);
        return getAutoView();
    }

    /**
     * 跳转 过滤条件页面
     *
     * @return 页面路径filter
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/filter")
    @Action(description = "业务数据模板中过滤条件页面跳转", exectype = "页面跳转日志")
    public String filterView(HttpServletRequest request, ModelMap modelMap) {
        String tableId = RequestUtil.getString(request, "tableId");
        Integer add = RequestUtil.getInt(request, "add");
        String selectAttrName = RequestUtil.getString(request, "selectAttrName");
        modelMap.put("tableId", tableId);
        modelMap.put("add", add);
        modelMap.put("selectAttrName", selectAttrName);
        return getAutoView();
    }

    /**
     * 跳转 设置分类
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/classify")
    @Action(description = "自定义表单设置分类页面跳转", exectype = "页面跳转日志")
    public String classify(HttpServletRequest request, ModelMap modelMap) {
        String formKeys = RequestUtil.getString(request, "formKeys");
        modelMap.put("formKeys", formKeys);
        return getAutoView();
    }

    /**
     * 跳转 导出页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/export")
    @Action(description = "自定义表单设置导出页面跳转", exectype = "页面跳转日志")
    public String export(HttpServletRequest request, ModelMap modelMap) {
        String formDefIds = RequestUtil.getString(request, "formDefIds");
        modelMap.put("formDefIds", formDefIds);
        return getAutoView();
    }

    /**
     * 跳转 业务数据模板页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/dataTemplate")
    @Action(description = "自定义表单业务数据模板页面跳转", exectype = "页面跳转日志")
    public String dataTemplate(HttpServletRequest request, ModelMap modelMap) {
        String tableId = RequestUtil.getString(request, "tableId");
        String formKey = RequestUtil.getString(request, "formKey");
        String formDefId = RequestUtil.getString(request, "formDefId");
        Integer dataTemplateCount = dataTemplateService.getCountByFormKey(formKey);
        modelMap.put("tableId", tableId);
        modelMap.put("formKey", formKey);
        modelMap.put("formDefId", formDefId);
        modelMap.put("dataTemplateCount", dataTemplateCount);
        return getAutoView();
    }

    /**
     * 跳转 添加页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/add")
    @Action(description = "自定义表单添加页面跳转", exectype = "页面跳转日志")
    public String add(HttpServletRequest request, ModelMap modelMap) {
        String categoryId = RequestUtil.getString(request, "categoryId");
        String categoryName = RequestUtil.getString(request, "categoryName");
        modelMap.put("categoryId", categoryId);
        modelMap.put("categoryName", categoryName);
        return getAutoView();
    }

    /**
     * 跳转 添加为菜单页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/resMenu")
    @Action(description = "自定义表单添加为菜单页面跳转", exectype = "页面跳转日志")
    public String resMenu(HttpServletRequest request, ModelMap modelMap) {
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 跳转 添加为菜单资源添加页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/resAdd")
    @Action(description = "自定义表单添加为菜单资源添加页面跳转", exectype = "页面跳转日志")
    public String resAdd(HttpServletRequest request, ModelMap modelMap) {
        String resId = RequestUtil.getString(request, "resId");
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("resId", resId);
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 跳转 业务数据保存页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/businessDataKeep")
    @Action(description = "自定义表单业务数据保存页面跳转", exectype = "页面跳转日志")
    public String businessDataKeep(HttpServletRequest request, ModelMap modelMap) {
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 跳转 表单设计页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/formDevise")
    @Action(description = "自定义表单表单设计页面跳转", exectype = "页面跳转日志")
    public String fromDevise(HttpServletRequest request, ModelMap modelMap) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        modelMap.put("formDefId", formDefId);
        return getAutoView();
    }

    /**
     * 跳转预览页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/formPreview")
    @Action(description = "自定义表单预览页面跳转", exectype = "页面跳转日志")
    public String formPreview(HttpServletRequest request, ModelMap modelMap) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        modelMap.put("formDefId", formDefId);
        return getAutoView();
    }

    /**
     * 跳转 左侧模板页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/formLeftTem")
    public String formLeftTem(HttpServletRequest request, ModelMap modelMap) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        modelMap.put("formDefId", formDefId);
        return getAutoView();
    }

    /**
     * 跳转 栅格1列
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/grid/grid1/index")
    public String grid1Index() {
        return getAutoView();
    }

    /**
     * 跳转 栅格2列
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/grid/grid2/index")
    public String grid2Index() {
        return getAutoView();
    }


    /**
     * 跳转 栅格3列
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/grid/grid3/index")
    public String grid3Index() {
        return getAutoView();
    }

    /**
     * 跳转标签页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/tag/index")
    public String tagIndex() {
        return getAutoView();
    }

    /**
     * 跳转标签属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/tag/props")
    public String tagProps() {
        return getAutoView();
    }

    /**
     * 跳转文本框页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/text/index")
    public String textIndex() {
        return getAutoView();
    }


    /**
     * 跳转按钮页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/button/index")
    public String buttonIndex() {
        return getAutoView();
    }

    /**
     * 跳转按钮属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/button/props")
    public String buttonProps() {
        return getAutoView();
    }

    /**
     * 跳转自定义按钮页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/button/customDialogTem")
    public String customDialogTem() {
        return getAutoView();
    }

    /**
     * 跳转文本框属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/text/props")
    public String textProps() {
        return getAutoView();
    }

    /**
     * 跳转流程定义选择器
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/actDefModel")
    public String actDefModel() {
        return getAutoView();
    }

    /**
     * 跳转到日期控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/date/index")
    public String templateDataIndex() {
        return getAutoView();
    }

    /**
     * 跳转到日期属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/date/props")
    public String dateProps() {
        return getAutoView();
    }

    /**
     * 跳转到隐藏域属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/hideField/props")
    public String hideFieldProps() {
        return getAutoView();
    }

    /**
     * 跳转到隐藏域页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/hideField/index")
    public String hideFieldIndex() {
        return getAutoView();
    }

    /**
     * 跳转到文本域控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/textArea/index")
    public String textAreaIndex() {
        return getAutoView();
    }

    /**
     * 跳转到文本域属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/textArea/props")
    public String textAreaProps() {
        return getAutoView();
    }

    /**
     * 跳转到下拉框控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/select/index")
    public String selectIndex() {
        return getAutoView();
    }

    /**
     * 跳转到下拉框属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/select/props")
    public String selectProps() {
        return getAutoView();
    }

    /**
     * 跳转到单选控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/radioOption/index")
    public String radioOptionIndex() {
        return getAutoView();
    }

    /**
     * 跳转到单选属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/radioOption/props")
    public String radioOptionProps() {
        return getAutoView();
    }


    /**
     * 跳转到复选控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/checkOption/index")
    public String checkOptionIndex() {
        return getAutoView();
    }

    /**
     * 跳转到复选属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/checkOption/props")
    public String checkOptionProps() {
        return getAutoView();
    }

    /**
     * 跳转到富文本控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/richText/index")
    public String richTextIndex() {
        return getAutoView();
    }


    /**
     * 跳转到富文本属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/richText/props")
    public String richTextProps() {
        return getAutoView();
    }


    /**
     * 跳转到表格控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/table/index")
    public String tableIndex() {
        return getAutoView();
    }

    /**
     * 跳转到表格单元格属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/table/props")
    public String tableProps() {
        return getAutoView();
    }

    /**
     * 跳转到上传文件控件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/uploadFile/index")
    public String uploadFileIndex() {
        return getAutoView();
    }

    /**
     * 跳转到附件属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/uploadFile/props")
    public String uploadFileProps() {
        return getAutoView();
    }

    /**
     * 跳转到选择器组件页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/selector/index")
    public String selectorIndex() {
        return getAutoView();
    }

    /**
     * 跳转到数据字典页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/dataLexi/index")
    public String dataLexiIndex() {
        return getAutoView();
    }

    /**
     * 跳转到数据字典属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/dataLexi/props")
    public String dataLexiProps() {
        return getAutoView();
    }

    /**
     * 跳转到上传图片页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/uploadImg/index")
    public String uploadImgIndex() {
        return getAutoView();
    }

    /**
     * 跳转到图片属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/uploadImg/props")
    public String uploadImgProps() {
        return getAutoView();
    }

    /**
     * 跳转到选择器页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/selector/selectorTme")
    public String selectorTme() {
        return getAutoView();
    }

    /**
     * 跳转到选择器属性页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formDesign/moulds/template/selector/props")
    public String selectorProps() {
        return getAutoView();
    }

    /**
     * 流程表单子表授权
     *
     * @param request http请求
     * @param model   mvc容器
     * @return 跳转路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "subRightsDialog")
    @Action(description = "流程表单子表授权", detail = "流程表单子表授权<#if success>失败<#else>成功</#if>")
    public String subRightsDialog(HttpServletRequest request, Model model) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String formKey = RequestUtil.getString(request, "formKey");
        String tableId = RequestUtil.getString(request, "tableId");
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId");

        // 添加操作日志
        boolean success = tableId == null;
        SysAuditThreadLocalHolder.putParamerter("success", success);
        model.addAttribute("procDefId", procDefId);
        model.addAttribute("nodeId", nodeId);
        model.addAttribute("formKey", formKey);
        model.addAttribute("tableId", tableId);
        model.addAttribute("parentProcDefId", parentProcDefId);
        return getAutoView();
    }

    /**
     * 跳转到选择模板页面
     *
     * @param request 请求
     * @param model   数据map
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/selectTemplate")
    @Action(description = "自定义表单选择模板页面跳转", exectype = "页面跳转日志")
    public String selectTemplate(HttpServletRequest request, ModelMap model) {
        String mainTableId = RequestUtil.getString(request, "mainTableId");
        String parentName = RequestUtil.getString(request, "parentName");
        String categoryId = RequestUtil.getString(request, "categoryId");
        List<FormTable> subTableList = formTableService.getSubTableByMainTableId(mainTableId);
        model.put("subList", subTableList);
        model.put("mainTableId", mainTableId);
        model.put("parentName", parentName);
        model.put("categoryId", categoryId);
        return getAutoView();
    }


    /**
     * 跳转 复制表单页面（克隆表单）
     * <pre>
     * 1、获取要复制的表单对象；
     * 2、重新生成表单ID（formDefId）、表单key（formKey）；
     * 3、打开复制表单的设置页面，让用户设置重新设置『表单标题』、『表单分类』、『表单描述』；
     * 4、保存以后，复制的表单和原表单使用同样的表，表可以添加字段和修改字段（修改的字段不能修改字段名和类型）。
     * </pre>
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/copy")
    @Action(description = "自定义表单复制表单页面跳转", exectype = "页面跳转日志")
    public String copy(HttpServletRequest request, ModelMap modelMap) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        FormDef bpmFormDef = null;
        if (StringUtil.isNotEmpty(formDefId)) {
            bpmFormDef = formDefService.getOneById(formDefId);
        }
        if (bpmFormDef != null) {
            //获取表单分类ID
            String cateId = bpmFormDef.getCategoryId();
            if (StringUtil.isNotEmpty(cateId)) {
                //根据表单分类ID获取分类
                GlobalType globalType = globalTypeService.getOneById(cateId);
                //分类名称
                bpmFormDef.setTypeName(globalType.getTypeName());
            }
        }
        modelMap.put("bpmFormDef", bpmFormDef);
        return getAutoView();
    }

    /**
     * 页面跳转 明细页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/detailed")
    @Action(description = "自定义表单明细页面跳转", exectype = "页面跳转日志")
    public String detailed(HttpServletRequest request, Model model) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        if (StringUtil.isNotEmpty(formDefId)) {
            FormDef formDef = formDefService.getOneById(formDefId);
            Date publishTime = formDef.getPublishTime();
            if (formDef.getIsPublished().equals(FormDef.IS_NOT_PUBLISHED)) {
                formDef.setTitle(DateFormatUtil.formatDate(formDef.getCreateTime()));
            }else{
                formDef.setTitle(DateFormatUtil.formatDate(publishTime));
            }
            model.addAttribute("formDef", formDef);
        }
        return getAutoView();
    }

    /**
     * 页面跳转 表单授权
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/formPermission")
    @Action(description = "自定义表单表单授权跳转", exectype = "页面跳转日志")
    public String rights(HttpServletRequest request, Model model) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId", "");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String formKey = RequestUtil.getString(request, "formKey");
        Integer platform = RequestUtil.getInt(request, "platform", 0);

        boolean isNodeRights = false;
        // 是否针对流程节点授权
        if (StringUtil.isNotEmpty(nodeId)) {
            List<BpmNodeSet> nodeSetList;
            if (StringUtil.isEmpty(parentProcDefId)) {
                nodeSetList = bpmNodeSetService.queryByProcDefId(procDefId);
            } else {
                nodeSetList = bpmNodeSetService.queryByProcDefIdAndParentProcDefId(procDefId, parentProcDefId, false);
            }

            model.addAttribute("bpmNodeSetList", nodeSetList);
            model.addAttribute("nodeId", nodeId);
            isNodeRights = true;
        }

        model.addAttribute("procDefId", procDefId);
        model.addAttribute("isNodeRights", isNodeRights);
        model.addAttribute("platform", platform);
        model.addAttribute("parentProcDefId", parentProcDefId);
        model.addAttribute("formKey", formKey);

        return getAutoView();
    }

    /**
     * 根据formKey获取表单权限
     *
     * @param request HttpServletRequest
     * @return Map<String , JSONObject>>
     */
    @ResponseBody
    @RequestMapping("/getPermissionByTableFormKey")
    @Action(description = "根据formKey获取表单权限", exectype = "查询日志",
            detail = "根据formKey获取表单权限<#if success>成功<#else>失败</#if>")
    public Map<String, List<JSONObject>> getPermissionByTableFormKey(HttpServletRequest request) {
        String formKey = RequestUtil.getString(request, "formKey");
        Integer platform = RequestUtil.getInt(request, "platform", 0);
        Map<String, List<JSONObject>> permission = null;
        try {
            if (StringUtil.isNotEmpty(formKey)) {
                permission = formRightsService.getPermission(formKey, null, null, null, platform);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return permission;
    }

    /**
     * 根据表和表单定义ID获取表单权限信息
     *
     * @param request 请求
     * @return Map
     */
    @ResponseBody
    @RequestMapping("/getPermissionByActDefId")
    @Action(description = "根据表和表单定义ID获取表单权限信息", exectype = "查询日志",
            detail = "根据表和表单定义ID获取表单权限信息<#if success>成功<#else>失败</#if>")
    public Map<String, List<JSONObject>> getPermissionByActDefId(HttpServletRequest request) {
        String formKey = RequestUtil.getString(request, "formKey");
        String actDefId = RequestUtil.getString(request, "actDefId");
        String parentActDefId = RequestUtil.getString(request, "parentActDefId", "");
        Integer platform = RequestUtil.getInt(request, "platform", 0);
        Map<String, List<JSONObject>> permission = null;
        try {
            if (StringUtil.isEmpty(parentActDefId)) {
                if (formKey.contains(StringPool.SLASH)) {
                    permission = formRightsService.getUrlFormPermission(request, formKey, actDefId, null, null, platform);
                } else {
                    permission = formRightsService.getPermission(formKey, actDefId, null, null, platform);
                }
            } else {
                permission = formRightsService.getPermission(formKey, actDefId, null, parentActDefId, platform);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return permission;
    }

    @ResponseBody
    @RequestMapping("/getPermissionByFormNode")
    @Action(description = "根据节点ID和表单定义ID获取表单权限信息", exectype = "查询日志",
            detail = "根据节点ID和表单定义ID获取表单权限信息<#if success>成功<#else>失败</#if>")
    public Map<String, List<JSONObject>> getPermissionByFormNode(HttpServletRequest request) {
        String formKey = RequestUtil.getString(request, "formKey");
        String actDefId = RequestUtil.getString(request, "actDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String parentActDefId = RequestUtil.getString(request, "parentActDefId", "");
        Integer platform = RequestUtil.getInt(request, "platform", 0);
        Map<String, List<JSONObject>> permission = null;
        try {
            if (StringUtil.isEmpty(parentActDefId)) {
                if (formKey.contains(StringPool.SLASH)) {
                    permission = formRightsService.getUrlFormPermission(request, formKey, actDefId, nodeId, null, platform);
                } else {
                    permission = formRightsService.getPermission(formKey, actDefId, nodeId, null, platform);
                }
            } else {
                permission = formRightsService.getPermission(formKey, actDefId, nodeId, parentActDefId, platform);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return permission;
    }


    /**
     * 保存表单权限
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping("/savePermission")
    @ResponseBody
    @Action(description = "保存表单权限", exectype = "添加或更新日志",
            detail = "保存表单权限<#if success>成功<#else>失败</#if>")
    public ResultData savePermission(HttpServletRequest request) {
        String permission = request.getParameter("permission");
        permission = StringEscapeUtils.unescapeHtml4(permission);
        String actDefId = RequestUtil.getString(request, "actDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String formKey = RequestUtil.getString(request, "formKey");
        String parentActDefId = RequestUtil.getString(request, "parentActDefId", "");
        Integer platform = RequestUtil.getInt(request, "platform", 0);
        JSONObject jsonObject = JSONObject.fromObject(permission);
        try {
            if (StringUtil.isNotEmpty(nodeId)) {
                // 设置节点权限。
                formRightsService.save(actDefId, nodeId, formKey, jsonObject, parentActDefId, platform);
            } else if (StringUtil.isNotEmpty(actDefId)) {
                // 设置流程全局权限。
                formRightsService.save(actDefId, null, formKey, jsonObject, parentActDefId, platform);
            } else {
                // 根据表单key保存权限。
                formRightsService.save(formKey, jsonObject, platform);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("formDef.formRights.save.success");
        } catch (Exception ex) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("formDef.formRights.save.fail");
        }
    }

    /**
     * 编辑自定义表单
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping("/edit")
    @ResponseBody
    @Action(description = "编辑自定义表单", exectype = "更新日志",
            detail = "编辑自定义表单<#if success>成功<#else>失败</#if>")
    public ResultData edit(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        Map<String, Object> retMap = new HashMap<>(16);
        try {
            if (StringUtil.isNotEmpty(formDefId)) {
                FormDef formDef = formDefService.getOneById(formDefId);
                retMap.put("formDef", formDef);
                retMap = formDefService.getEditDataByFormDef(formDef, retMap);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("operation.success", retMap);
    }

    /**
     * 导出自定义表单XML
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @RequestMapping("/exportXml")
    @Action(description = "导出自定义表单", exectype = "导出日志",
            detail = "导出自定义表单<#if success>成功<#else>失败</#if>")
    public void exportXml(HttpServletRequest request, HttpServletResponse response) {

        String[] formDefIds = RequestUtil.getStringAryByStr(request, "formDefIds");
        Map<String, Boolean> map = new HashMap<>(8);
        map.put(XmlConstant.BPM_FORM_DEF, true);
        map.put("bpmFormTable", RequestUtil.getBoolean(request, "bpmFormTable"));
        map.put("bpmFormDefOther", RequestUtil.getBoolean(request, "bpmFormDefOther"));
        map.put("bpmFormRights", RequestUtil.getBoolean(request, "bpmFormRights"));
        map.put("bpmTableTemplate", RequestUtil.getBoolean(request, "bpmTableTemplate"));
        map.put("sysBusEvent", RequestUtil.getBoolean(request, "sysBusEvent"));
        map.put("formDefTree", RequestUtil.getBoolean(request, "formDefTree"));
        if (BeanUtils.isEmpty(formDefIds)) {
            return;
        }
        try {
            String fileName = DateFormatUtil.getNowByString("yyyyMMddHHmmdd") + ".xml";
            if (formDefIds.length == 1) {
                FormDef formDef = formDefService.getOneById(formDefIds[0]);
                fileName = formDef.getSubject() + "_" + fileName;
            } else {
                fileName = FormConstants.STR_FORM_RECORDS + fileName;
            }
            String strXml = formDefService.exportXml(formDefIds, map);
            FileUtil.downLoad(request, response, strXml, fileName);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
    }

    /**
     * 导入自定义表单的XML
     *
     * @param request  HttpServletRequest
     * @param tenantId 租户id
     * @return ResultData
     */
    @RequestMapping("/importXml")
    @ResponseBody
    @Action(description = "导入自定义表单", exectype = "导入日志",
            detail = "导入自定义表单<#if success>成功<#else>失败</#if>")
    public ResultData importXml(MultipartHttpServletRequest request, @RequestParam("tenantId") String tenantId) {
        if (StringUtil.isNotEmpty(tenantId)) {
            String[] splitStr = tenantId.split(",");
            if (splitStr.length > 0) {
                tenantId = splitStr[0];
            }
        }
        MultipartFile file = request.getFile("file");
        try {
            if (null != file) {
                formTableService.importXmlCommonCheck(tenantId, file);
                formDefService.importXml(file.getInputStream(), tenantId);
                SysAuditThreadLocalHolder.putParamerter("success", true);
                return getResultDataSuccess("formDef.importXml.success");
            }
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail(e.getMessage());
        }
        return getResultDataError("operation.failure");
    }

    /**
     * 给表单设置分类
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping("/setCategory")
    @ResponseBody
    @Action(description = "给表单设置分类", exectype = "更新日志",
            detail = "给表单设置分类<#if success>成功<#else>失败</#if>")
    public ResultData setCategory(HttpServletRequest request) {
        String categoryId = RequestUtil.getString(request, "categoryId");
        String formKeys = RequestUtil.getString(request, "formKeys");
        if (StringUtil.isEmpty(categoryId)) {
            return getResultDataError("formDef.setCategory.notCategoryId");
        }
        if (StringUtil.isEmpty(formKeys)) {
            return getResultDataError("formDef.setCategory.notSelectForm");
        }
        String[] arrayFormKey = formKeys.split(",");
        List<String> formKeyList = new ArrayList<>();
        Collections.addAll(formKeyList, arrayFormKey);
        try {
            formDefService.updCategory(categoryId, formKeyList);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success");
        } catch (Exception ex) {
            ex.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("formDef.setCategory.fail");
        }
    }

    /**
     * 保存克隆
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping("/saveCopy")
    @ResponseBody
    @Action(description = "复制表单", exectype = "添加日志",
            detail = "复制表单<#if success>成功<#else>失败</#if>")
    public ResultData saveCopy(HttpServletRequest request) {
        //原表单id
        String formDefId = RequestUtil.getString(request, "formDefId");
        //表单新的名称
        String formName = RequestUtil.getString(request, "formName");
        //表单新的key
        String formKey = RequestUtil.getString(request, "formKey");
        //分类id
        String typeId = RequestUtil.getString(request, "typeId");
        //表单描述
        String formDesc = RequestUtil.getString(request, "formDesc");
        //根据原表单id，取得表单所有信息
        FormDef bpmFormDef = formDefService.getOneById(formDefId);
        //判断新的表单别名是否已经存在
        int count = formDefService.getCountByFormKey(formKey);
        if (count > 0) {
            return getResultDataError("formDef.alias.repeat");
        }
        //获取原表单的key，留着备用
        String oldFormKey = bpmFormDef.getFormKey();
        if (BeanUtils.isNotEmpty(bpmFormDef)) {
            //判断，如果新的表单名称不为空
            if (!StringUtil.isEmpty(formName)) {
                //设置表单标题
                bpmFormDef.setSubject(formName);
            }
            if (StringUtil.isNotEmpty(typeId)) {
                //设置分类id
                bpmFormDef.setCategoryId(typeId);
            }
            if (!StringUtil.isEmpty(formDesc)) {
                //设置表单描述
                bpmFormDef.setFormDesc(formDesc);
            }
            //为复制的表单生成新的表单id
            String id = UUIDUtils.getUUIDFor32();
            bpmFormDef.setFormDefId(id);
            bpmFormDef.setFormKey(formKey);
            bpmFormDef.setIsPublished((short) 0);
            bpmFormDef.setIsDefault((short) 1);
            bpmFormDef.setVersionNo(1);
            try {
                formDefService.copyForm(bpmFormDef, oldFormKey);
                SysAuditThreadLocalHolder.putParamerter("success", true);
                return getResultDataSuccess("formDef.copy.success");
            } catch (Exception ex) {
                ex.printStackTrace();
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError("formDef.copy.fail");
            }
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("formDef.getForm.fail");
        }
    }

    /**
     * 发布表单
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping("/publish")
    @ResponseBody
    @Action(description = "发布表单", exectype = "添加日志",
            detail = "发布表单<#if success>成功<#else>失败</#if>")
    public ResultData publish(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        try {
            formDefService.publish(formDefId, ContextUtil.getCurrentUser().getFullName());
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("formDef.publish.success");
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("formDef.publish.fail");
        }
    }

    /**
     * 业务数据模板-过滤条件 添加请求数据
     *
     * @return ResultData
     */
    @RequestMapping("/filter")
    @ResponseBody
    @Action(description = "业务数据模板过滤条件添加请求数据", exectype = "添加或更新日志",
            detail = "业务数据模板过滤条件添加请求数据<#if success>成功<#else>失败</#if>")
    public ResultData filter(HttpServletRequest request) {
        String tableId = RequestUtil.getString(request, "tableId");
        Map<String, Object> map;
        try {
            FormTable bpmFormTable = dataTemplateService.getFieldListByTableId(tableId);
            String source = (bpmFormTable.getIsExternal() == FormTable.NOT_EXTERNAL) ? DataTemplate.SOURCE_CUSTOM_TABLE : DataTemplate.SOURCE_OTHER_TABLE;
            List<CommonVar> commonVars = CommonVar.getCurrentVars(false);
            map = new HashMap<>(4);
            map.put("commonVars", commonVars);
            map.put("bpmFormTable", bpmFormTable);
            map.put("tableId", tableId);
            map.put("source", source);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("operation.success", map);
    }

    /**
     * 添加或更新自定义表单
     *
     * @param request 请求
     * @return ResultData
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @ResponseBody
    @Action(description = "添加或更新自定义表单", exectype = "添加或更新日志",
            detail = "添加或更新自定义表单<#if success>成功<#else>失败</#if>")
    public ResultData save(HttpServletRequest request) {
        String subject = RequestUtil.getString(request, "subject");
        String formKey = RequestUtil.getString(request, "formKey");
        String formDesc = RequestUtil.getString(request, "formDesc");
        String categoryId = RequestUtil.getString(request, "categoryId");
        Short isPublic = RequestUtil.getShort(request, "isPublic");
        int designType = RequestUtil.getInt(request, "designType");
        String templatesId = RequestUtil.getString(request, "templatesId");
        String subTemplateField = RequestUtil.getString(request, "subTemplateField");
        String mainTableId = RequestUtil.getString(request, "mainTableId");
        subTemplateField = StringEscapeUtils.unescapeHtml4(subTemplateField);
        FormDef formDef = new FormDef();
        formDef.setTableId(mainTableId);
        formDef.setSubject(subject);
        formDef.setFormKey(formKey);
        formDef.setFormDesc(formDesc);
        formDef.setIsPublic(isPublic);
        formDef.setDesignType(designType);
        formDef.setTemplatesId(templatesId);
        formDef.setSubTemplateField(subTemplateField);
        formDef.setCategoryId(categoryId);
        SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
        formDef.setCreator(curUser.getAccount());
        formDef.setCreateBy(curUser.getUserId());
        formDef.setIsDefault(FormDef.IS_DEFAULT);
        formDef.setVersionNo(FormDef.VERSION_NO_DEFAULT);
        formDef.setCreateTime(new Date());
        formDef.setHtml("");
        String formDefId = "";
        if (StringUtil.isNotEmpty(formDef.getFormDefId())) {
            formDefId = formDef.getFormDefId();
        }
        //表单添加
        boolean isAdd = StringUtil.isEmpty(formDef.getFormDefId());
        if (isAdd) {
            int rtn = formDefService.getCountByFormKey(formKey);
            if (rtn > 0) {
                return getResultDataError("formDef.save.formKeyValid");
            }
        }
        try {
            if (isAdd) {
                formDefId = formDefService.addForm(formDef);
            } else {
                formDefService.updateForm(formDef);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("save.failure");
        }
        return getResultDataSuccess("save.success", formDefId);
    }

    /**
     * 保存表单的html片段
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @PostMapping("/saveFormHtml")
    @ResponseBody
    @Action(description = "保存表单的html片段", exectype = "添加日志",
            detail = "保存表单的html片段<#if success>成功<#else>失败</#if>")
    public ResultData saveFormHtml(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        String html = RequestUtil.getString(request, "html");
        try {
            FormDef formDef = formDefService.getOneById(formDefId);
            SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
            if (BeanUtils.isNotEmpty(curUser)) {
                formDef.setPublishedBy(curUser.getFullName());
            }
            formDef.setPublishTime(new Date());
            html = html.replace("？", "");
            String decodeHtml = URLDecoder.decode(URLDecoder.decode(html.replaceAll("%;", "%25;"), "utf-8"), "utf-8");
            String template = FormUtil.getFreeMarkerTemplate(decodeHtml, formDef.getTableId());
            formDef.setTemplate(template);
            formDef.setHtml(html);
            formDefService.updateForm(formDef);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("update.failure");
        }
        return getResultDataSuccess("update.success");
    }

    /**
     * 取得自定义表单的分页列表
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "取得自定义表单的分页列表", exectype = "查询日志",
            detail = "取得自定义表单的分页列表<#if success>成功<#else>失败</#if>")
    public ResultData list(HttpServletRequest request) {
        QueryFilter filter;
        List<FormDef> formDefList;
        try {
            SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
            String account = curUser.getAccount();
            String tenantId = ContextUtil.getCurrentUserTenantId();
            filter = new QueryFilter(request, true);
            filter.put("creator", account);
            filter.put("tenantId", tenantId);
            formDefList = formDefService.queryList(filter);
            for (FormDef formDef : formDefList) {
                String formKey = formDef.getFormKey();
                Integer versionCount = formDefService.getCountByFormKey(formKey);
                formDef.setVersionCount(versionCount);
                Integer dataTemplateCount = dataTemplateService.getCountByFormKey(formKey);
                formDef.setDataTemplateCount(dataTemplateCount);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
        return getResultDataSuccess("query.success", filter.getPageBean().getTotalCount(), formDefList);
    }

    /**
     * 新建表单版本
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping("/newVersion")
    @ResponseBody
    @Action(description = "新建表单版本", exectype = "添加日志",
            detail = "新建表单版本<#if success>成功<#else>失败</#if>")
    public ResultData newVersion(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        try {
            String newFormDefId = UUIDUtils.getUUIDFor32();
            formDefService.newVersion(formDefId, newFormDefId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("formDef.newVersion.success");
        } catch (Exception ex) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("formDef.newVersion.failure");
        }
    }

    /**
     * 跳转到表单所有版本页面
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/versions")
    @Action(description = "跳转到表单所有版本页面", exectype = "页面跳转日志")
    public String formDefViewVersions(HttpServletRequest request, ModelMap modelMap) {
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 获取到表单版本数据
     *
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/versions")
    @ResponseBody
    @Action(description = "获取到表单版本数据", exectype = "查询日志",
            detail = "获取到表单版本数据<#if success>成功<#else>失败</#if>")
    public ResultData versions(HttpServletRequest request) {
        String formKey = RequestUtil.getString(request, "formKey");
        //版本信息
        List<FormDef> list;
        try {
            list = formDefService.getByFormKey(formKey);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
        return getResultDataSuccess("query.success", list);
    }

    /**
     * 设置当前表单版本为默认版本
     *
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/setDefaultVersion")
    @ResponseBody
    @Action(description = "设置当前表单版本为默认版本", exectype = "更新日志",
            detail = "设置当前表单版本为默认版本<#if success>成功<#else>失败</#if>")
    public ResultData setDefault(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        try {
            formDefService.setDefaultVersion(formDefId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("formDef.setDefault.success");
    }

    /**
     * 跳转 设置是否公开
     *
     * @param request  request
     * @param modelMap ModelMap
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/setPublic")
    @Action(description = "跳转到设置是否公开页面", exectype = "页面跳转日志")
    public String setPublic(HttpServletRequest request, ModelMap modelMap) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        Short isPublic = RequestUtil.getShort(request, "isPublic");
        modelMap.put("formDefId", formDefId);
        modelMap.put("isPublic", isPublic);
        return getAutoView();
    }

    /**
     * 设置当前表单版本为公开
     * 存在偶尔修改不成功的bug，数据能传到后台
     *
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/setPublic")
    @ResponseBody
    @Action(description = "设置当前表单版本为公开", exectype = "更新日志",
            detail = "设置当前表单版本为公开<#if success>成功<#else>失败</#if>")
    public ResultData setPublic(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        short isPublic = RequestUtil.getShort(request, "isPublic");
        try {
            formDefService.setPublic(formDefId, isPublic);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("formDef.set.success");
    }

    /**
     * 跳转 树结构页面
     *
     * @param request  request
     * @param modelMap modelMap
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/treeStructure")
    @Action(description = "跳转到树结构页面", exectype = "页面跳转日志")
    public String treeStructure(HttpServletRequest request, ModelMap modelMap) {
        String formKey = RequestUtil.getString(request, "formKey");
        modelMap.put("formKey", formKey);
        return getAutoView();
    }

    /**
     * 删除自定义表单
     *
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/delByFormKey")
    @ResponseBody
    @Action(description = "删除自定义表单", exectype = "删除日志",
            detail = "删除自定义表单<#if success>成功<#else>失败</#if>")
    public ResultData delByFormKey(HttpServletRequest request) {
        String[] aryFormKey = RequestUtil.getStringAryByStr(request, "formKey");
        try {
            List<String> bindFlowFormList = formDefService.delByFormKeys(aryFormKey);
            if (bindFlowFormList.size() > 0) {
                //多个表单被关联，提示被关联的表单名列表
                return getResultDataError("formDef.delByFormKey.flow", 0, bindFlowFormList.toString());
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
        return getResultDataSuccess("delete.success");
    }

    /**
     * 根据表单id删除自定义表单
     *
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/delByFormDefId")
    @ResponseBody
    @Action(description = "删除自定义表单", exectype = "删除日志",
            detail = "删除自定义表单<#if success>成功<#else>失败</#if>")
    public ResultData delByFormId(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        try {
            formDefService.delById(formDefId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
        return getResultDataSuccess("delete.success");
    }

    /**
     * 重置表单权限设置
     *
     * @param request HttpServletRequest
     */
    @ResponseBody
    @RequestMapping("/resetPermission")
    @Action(description = "重置表单权限设置", exectype = "更新日志",
            detail = "重置表单权限设置<#if success>成功<#else>失败</#if>")
    public ResultData initRights(HttpServletRequest request) {
        PlatformType platformType = PlatformType.getEnumFromString(RequestUtil.getString(request, "platform", "PC"));
        try {
            String formKey = RequestUtil.getString(request, "formKey");
            String nodeId = RequestUtil.getString(request, "nodeId");
            String actDefId = RequestUtil.getString(request, "actDefId");
            String parentActDefId = RequestUtil.getString(request, "parentActDefId");
            formRightsService.deleteRight(formKey, actDefId, nodeId, parentActDefId, Integer.parseInt(platformType.toString()));
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception ex) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("formDef.resetPermission.failure");
        }
        return getResultDataSuccess("formDef.resetPermission.success");
    }

    /**
     * 根据主表id获取主表和子表数据
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping("/getMainSubInfo")
    @ResponseBody
    @Action(description = "根据主表ID获取主表和子表数据", exectype = "查询日志",
            detail = "根据主表ID获取主表和子表数据<#if success>成功<#else>失败</#if>")
    public ResultData getMainSubInfo(HttpServletRequest request) {
        String mainTableId = RequestUtil.getString(request, "mainTableId");
        Map<String, Object> retMap = new HashMap<>(16);
        try {
            List<FormTable> subTableList = formTableService.getSubTableByMainTableId(mainTableId);
            List<Map> retSubList = new ArrayList<>();
            List<String> subIds = new ArrayList<>();
            for (FormTable subTable : subTableList) {
                String tableId = subTable.getTableId();
                Map<String, Object> subMap = new HashMap<>(16);
                List<FormField> fields = formFieldService.getByTableId(tableId);
                subMap.put("subTableId", subTable.getTableId());
                subMap.put("fields", fields);
                retSubList.add(subMap);
                subIds.add(subTable.getTableId());
            }
            retMap.put("subIds", subIds);
            retMap.put("subList", retSubList);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("operation.success", retMap);
    }
}
